package com.docker.sys.user;

import com.docker.core.mvc.service.BaseService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Okv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import kit.docker.convert.RecordKit;
import kit.docker.security.PwdEncoderKit;

/**
 * @Author ldl
 * @Create 2020/09/17 017 09:55
 */
public class InsideUserService extends BaseService {

    @Inject
    private InsideUserDao insideUserDao;

//    @Inject
//    private InsideUserRoleService roleService;

    public Page<Record> paginate(Okv cond, int pageNum, int pageSize) {
        return insideUserDao.paginate(cond, pageNum, pageSize);
    }

    public boolean save(InsideUserBean bean, String userId) {
        if (insideUserDao.exist("-1", bean.getUser_phone())) {
            throwError("手机号已存在");
        }
        bean.setUser_id(getUUID32());
        bean.setPassword(PwdEncoderKit.encrytion("pajx123abc"));
        bean.setStatus_flag("0");
        bean.setRemark("新增用户");
        bean.setCreate_time(getNowDateTime());
        bean.setCreate_oper_id(userId);
        bean.setUpdate_time(bean.getCreate_time());
        bean.setUpdate_oper_id(bean.getCreate_oper_id());
        bean.setIs_admin("0");
        return insideUserDao.save(RecordKit.objectToRecord(bean));
    }

    public boolean update(InsideUserBean bean, String userId) {
        Record user = info(bean.getUser_id());
        if (!StrKit.notNull(user)) {
            throwError("用户不存在");
        }
        user.set("remark", "修改用户");
        user.set("user_name", bean.getUser_name());
        user.set("user_sex", bean.getUser_sex());
        user.set("update_time", getNowDateTime());
        user.set("update_oper_id", userId);
        return insideUserDao.update(user);
    }

    private Record info(String userId) {
        return insideUserDao.info(userId);
    }

    public boolean enable(String userId, String loginUserId) {
        Record user = info(userId);
        if (!StrKit.notNull(user)) {
            throwError("用户不存在");
        }
        if ("1".equals(user.getStr("status_flag"))) {
            throwError("用户已启用");
        }
//        if (!roleService.exist(userId)) {
//            throwError("用户未分配角色");
//        }
        user.set("remark", "启用用户");
        user.set("status_flag", "1");
        user.set("update_time", getNowDateTime());
        user.set("update_oper_id", loginUserId);
        return insideUserDao.update(user);
    }

    public boolean disable(String userId, String loginUserId) {
        Record user = info(userId);
        if (!StrKit.notNull(user)) {
            throwError("用户不存在");
        }
        if (!"1".equals(user.getStr("status_flag"))) {
            throwError("用户已停用");
        }
        user.set("remark", "停用用户");
        user.set("status_flag", "0");
        user.set("update_time", getNowDateTime());
        user.set("update_oper_id", loginUserId);
        return insideUserDao.update(user);
    }

}
